<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="searchInpt"
        placeholder="输入设备码/ID/手机号"
        clearable
        @keyup.enter.native="search()"
        class="search-btn"
      >
        <el-button
          @click="search()"
          icon="el-icon-search"
          slot="append"
          :loading="searchLoading"
        >搜索</el-button>
      </el-input>
      <!-- <div class="lz-tips">
        <label>
          排序:
        </label>
      </div>
      <el-select
        v-model="bindingTimeValue"
        placeholder="排序"
        @change="search()"
      >
        <el-option
          v-for="item in bindingTimeSort"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select> -->
    </div>
    <div class="table-container">
      <Table
        :table="dataTable"
        @doEquipmentUnbindData="doEquipmentUnbindData"
      >
        <template
          slot-scope="props"
          slot="bandingHistory"
        >
          <el-button
            type="primary"
            size="small"
            @click="goHistory(props.obj.row)"
          >查看</el-button>
        </template>
      </Table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          v-if="pageshow"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import {
  fetchGetEquipmentManageData,
  fetchEquipmentUnbindData,
  fetchGetEquipmentSortData
} from "@/api/table";
import { formatDateTime } from "@/utils/common";
import Table from "@/components/Table";
import format from "date-format";
export default {
  data() {
    return {
      count: 0,
      limit: 20,
      currentPage: 1,
      searchInpt: "",
      searchLoading: false,
      dataTable: {
        loading: false,
        border: true,
        hasOperation: true,
        tr: [
          {
            id: "1",
            label: "设备编号",
            prop: "hardwareEquipment",
            show: true
          },
          {
            id: "7",
            label: "批次",
            prop: "batchNumber",
            show: true
          },
          {
            id: "2",
            label: "ID",
            prop: "userId",
            show: true
          },
          {
            id: "6",
            label: "绑定历史",
            prop: "bandingHistory",
            show: "template"
          }
        ],
        data: [],
        operation: {
          label: "操作",
          data: [
            {
              label: "解绑", // 按钮文字
              Fun: "doEquipmentUnbindData", // 点击按钮后触发的父组件事件
              size: "mini", // 按钮大小
              id: "1" // 按钮循环组件的key值
            }
          ]
        }
      },
      equipmentData: [],
      pageshow: true,
      bindingTimeSort: [
        {
          value: "1",
          label: "按绑定次数排序"
        },
        {
          value: "2",
          label: "按绑定时间排序"
        }
      ],
      bindingTimeValue: "1"
    };
  },
  mounted() {
    this.getEquipmentManageData();
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getEquipmentManageData();
    },
    search() {
      this.currentPage = 1;
      this.pageshow = false;
      this.getEquipmentManageData();
      this.$nextTick(() => {
        this.pageshow = true;
      });
    },
    //时间格式化
    formatterTime(t) {
      if (t) {
        const newTime = format("yyyy-MM-dd hh:mm:ss", new Date(t)).split(" ");
        let time = "";
        time += newTime[0] + "\n" + newTime[1];
        return time;
      } else {
        return "";
      }
    },
    async getEquipmentManageData() {
      this.dataTable.loading = true;
      const manage = await fetchGetEquipmentManageData({
        pageSize: this.limit,
        pageNo: this.currentPage,
        searchKey: this.searchInpt,
        sortKey: this.bindingTimeValue
      });
      this.dataTable.loading = false;
      this.dataTable.data = [];
      this.count = manage.data.total;
      if (manage.data.list) {
        manage.data.list.forEach(item => {
          const manageTable = {};
          manageTable.bandingCount = item.bandingCount;
          manageTable.batchNumber = item.batchNumber;
          manageTable.bandingTime = this.formatterTime(item.bandingTime);
          manageTable.lastBandingTime = this.formatterTime(
            item.lastBandingTime
          );
          manageTable.hardwareEquipment = item.hardwareEquipment;
          manageTable.nickName = item.nickName;
          manageTable.userId = item.userId;
          manageTable.username = item.username;
          this.dataTable.data.push(manageTable);
        });
      }
    },
    doEquipmentUnbindData(index, row) {
      this.$confirm("确认解除绑定", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.equipmentUnbindData(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    async equipmentUnbindData(data) {
      const unbind = await fetchEquipmentUnbindData({
        hardwareEquipment: data.hardwareEquipment
      });
      if (unbind.status == "200") {
        this.getEquipmentManageData();
        this.$message({
          type: "success",
          message: "删除成功"
        });
      } else {
        this.$message({
          type: "error",
          message: unbind.msg
        });
      }
    },
    changeBindingTimeValue() {
      this.getEquipmentManageData();
    },
    goHistory(data) {
      this.$router.push({
        path: "equipmentHistorybind",
        query: { hardwareEquipment: data.hardwareEquipment }
      });
    }
  },
  components: {
    Table
  },
  computed: {
    ...mapGetters(["permission_roles"])
  }
};
</script>
<style lang="less" scoped>
.search_box {
  padding: 20px 0;
}
</style>

